<?php
class o2o_analysis_store_daliy{

    public function generate(){
        $nowDay = date("Y-m-d",strtotime("-1 day"));
        $begin_unixtime = strtotime($nowDay);
        $end_unixtime = strtotime($nowDay.' 23:59:59');

        //获取配送自提、配送的物流公司信息
        $dlyCorpObj = app::get('ome')->model('dly_corp');
        $dlyCorpArr = $dlyCorpObj->getList('corp_id,type', array('d_type'=>2), 0, -1);
        foreach($dlyCorpArr as $dlyCorp){
            if($dlyCorp['type'] == 'o2o_ship'){
                $o2o_ship_logi_id = $dlyCorp['corp_id'];
            }elseif($dlyCorp['type'] =='o2o_pickup'){
                $o2o_pickup_logi_id = $dlyCorp['corp_id'];
            }
        }
        unset($dlyCorpArr);

        //获取当前系统门店以及门店仓的相关信息
        $storeObj = app::get('o2o')->model('store');
        $storeArr = $storeObj->getList('store_bn,name,branch_id', array(), 0, -1);
        foreach($storeArr as $store){
            $branch_ids[] = $store['branch_id'];
            $storeInfo[$store['branch_id']] = array('store_bn'=>$store['store_bn'],'store_name'=>$store['name']);
        }
        unset($storeArr);

        $storeDaliyObj = app::get('o2o')->model('store_daliy');

        //按门店仓的维度统计相关信息
        foreach($branch_ids as $branch_id){
            $store_daliy = $storeInfo[$branch_id];
            $store_daliy['createtime'] = $begin_unixtime;

            //订单总数
            $sql = "SELECT count(delivery_id) AS order_sum FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND create_time >=".$begin_unixtime." AND create_time <=".$end_unixtime;
            $data = $storeObj->db->selectrow($sql);
            $store_daliy['order_sum'] = $data['order_sum'];

            //审核单量
            $sql = "SELECT count(delivery_id) AS confirm_num FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND last_modified >=".$begin_unixtime." AND last_modified <=".$end_unixtime." AND confirm=1";
            $data = $storeObj->db->selectrow($sql);
            $store_daliy['confirm_num'] = $data['confirm_num'];

            //拒绝单量
            $sql = "SELECT count(delivery_id) AS refuse_num FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND last_modified >=".$begin_unixtime." AND last_modified <=".$end_unixtime." AND confirm=2";
            $data = $storeObj->db->selectrow($sql);
            $store_daliy['refuse_num'] = $data['refuse_num'];

            //发货单量
            $sql = "SELECT count(delivery_id) AS send_num FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND delivery_time >=".$begin_unixtime." AND delivery_time <=".$end_unixtime." AND status=3";
            $data = $storeObj->db->selectrow($sql);
            $store_daliy['send_num'] = $data['send_num'];

            if($store_daliy['send_num'] > 0){
                //销售货品数
                $sql = "SELECT SUM(wdi.number) AS sale_sum FROM sdb_wap_delivery_items AS wdi LEFT JOIN sdb_wap_delivery AS wd on wdi.delivery_id = wd.delivery_id WHERE wd.branch_id =".$branch_id." AND wd.delivery_time >=".$begin_unixtime." AND wd.delivery_time <=".$end_unixtime." AND wd.status=3";
                $data = $storeObj->db->selectrow($sql);
                $store_daliy['sale_sum'] = $data['sale_sum'];

                //配送占比
                $sql = "SELECT count(delivery_id) AS distribution_num FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND delivery_time >=".$begin_unixtime." AND delivery_time <=".$end_unixtime." AND status=3 AND logi_id=".$o2o_ship_logi_id;
                $data = $storeObj->db->selectrow($sql);
                $store_daliy['distribution_rate'] = $data['distribution_num'] ? $data['distribution_num']/$store_daliy['send_num'] : 0.0000;

                //自提占比
                $sql = "SELECT count(delivery_id) AS self_pick_num FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND delivery_time >=".$begin_unixtime." AND delivery_time <=".$end_unixtime." AND status=3 AND logi_id=".$o2o_pickup_logi_id;
                $data = $storeObj->db->selectrow($sql);
                $store_daliy['self_pick_rate'] = $data['self_pick_num'] ? (1-$store_daliy['distribution_rate']) : 0.0000;
            }else{
                $store_daliy['sale_sum'] = 0;
                $store_daliy['distribution_rate'] = 0.0000;
                $store_daliy['self_pick_rate'] = 0.0000;
            }

            //核销签收单量
            $sql = "SELECT count(delivery_id) AS verified_num FROM sdb_wap_delivery WHERE branch_id =".$branch_id." AND last_modified >=".$begin_unixtime." AND last_modified <=".$end_unixtime." AND is_received=2";
            $data = $storeObj->db->selectrow($sql);
            $store_daliy['verified_num'] = $data['verified_num'];

            $storeDaliyObj->insert($store_daliy);
            unset($store_daliy);
        }
    }
}
